define(['jquery','api','user','util'],function($,api,user,util){
	var vm,
	   channels=[],
	   userfunds={},
	   controller = {
	   init:function(id){
	   	   if(!user.checkLogin())return;
		   api.initFooter(3);
		   api.setTitle('提现');
		   if(!vm){
		     this.render();
		   }else{
		     vm.show=true;
		   }
		   this.initChannel();
		   this.loadAllowFund();
	   },
	   render:function(){
	   	   $('#pages').append('<div id="export"></div>');
	       require(['text!pages/fund/manual.html','Vue'],function(tpl,Vue){
	            vm=new Vue({
	               el:'#export',
	               data:{
	               	  show:true,
	               	  user:{allowFund:0},
	               	  channel:false,
		              pwd:'',
		              outMoney:''
	               },
	               filters:{
	       	         	priceFormat:function(v){
	       	         	    return v?util.priceFormat(2,v):'';
	       	         	}
	       	       },
	               methods:{
	                  cash:function(){
		                	var data={
		                	   amount:this.outMoney*100,
		                	   channel:this.channel.channelid,
		                	   moneypwd:this.pwd
		                	};
		                	if(this.channel){
		                		if(data.amount==''){
		                		   util.msg('输入金额格式不正确！');
		                		   return;
		                		}else if(data.moneypwd==''){
		                		   util.msg('请输入密码！');
		                		   return;
		                		}else if(parseInt(data.money)>this.user.allowFund){
		                		   util.msg('提现金额不能大于账户余额！');
		                		   return
		                		}
		                	    api.cash(data,function(rs){
		                	        if(rs.retcode=='0'){
		                	            util.msg('提现申请成功！');
		                	            setTimeout(function(){history.go(-1)},500)
		                	        }else{
		                	            util.dealErrorCode(rs.retcode);
		                	        }
		                	    });
		                	}
		                }
	               },
	               template:tpl
	            })
	       })
	   },
	   loadAllowFund:function(){
	      api.allowFund(function(rs){
	          if(rs.retcode=='0'){
	               vm.user.allowFund=rs.allowoutfunds
	          }else{
	              util.msg('获取用户可提现资金失败！');
	          }
	      })
	   },
	   initChannel:function(){
	   	   var userchannels,
	   	       channels,
	   	       channelHash={},
	   	       channel,
	   	       cb=function(){
	   	       	  for(var i=0;i<userchannels.length;i++){
	   	       	  	   channel=userchannels[i];
	   	       	       channelHash[channel.channelid]=channel;
	   	       	  }
	   	       	  for(var i=0;i<channels.length;i++){
	   	       	  	   channel=channels[i];
	   	       	       if(+channel.outmode>50){
	   	       	       	  break;
	   	       	       }
	   	       	  }
	           	  if(channel&&channelHash[channel.channelid]&&channelHash[channel.channelid].bankaccount!=''){
	              	vm.channel=channelHash[channel.channelid];
	           	  }else{
	           	  	util.msg('还没有绑定银行卡，现在转向绑卡页面！');
	           	    setTimeout(function(){location.hash='#/fund/bind/'+channel.channelid;},500);
	           	  }
	   	      }
	   	       	  
	   	   api.channels(function(rs){
	           if(rs.retcode=='0'){
	           	  channels=rs.data;
	           	  if(userchannels)cb();
	           }
	       });
	   	   api.userchannels(function(rs){
	           if(rs.retcode=='0'){
	           	  userchannels=rs.data;
	           	  if(channels)cb();
	           }
	       });
	   },
	   onRouteChange:function(){
	       if(vm)vm.show=false;
	   }
	};
    return controller;
})